﻿/// <reference path="../../../utilities/FXDom.js" />
/// <reference path="../../../utilities/Post.js" />
/// <reference path="../../../utilities/Thread.js" />
/// <reference path="MarkupElementSelector.js" />
///--------------------------------------------------------------------
/// <file name="BlogMarkupElementSelector.js">
///   A markup element selector for blogs.
/// </file>
///--------------------------------------------------------------------

NAMFox.ns(function() { with (NAMFox) {

this.BlogMarkupElementSelector = function(htmlDocument, author) {
    /// <summary>
    /// Initializes a new instance of the ViewThreadMarkupElementSelector class.
    /// </summary>
    /// <param name="htmlDocument" type="nsIDOMHTMLDocument">
    /// The document object associated with the current page.
    /// </param>
    /// <param name="author" type="String" optional="true">
    /// (Optional) The name of the author of the post, if applicable.
    /// This does not apply in cases where there are multiple blog
    /// posts by different authors on the same page.
    /// </param>
    
    this._htmlDocument = htmlDocument;
    this._author = author && author.replace(/\+/g, " ").replace(/%20/g, " ") || null;
};

BlogMarkupElementSelector.prototype = new MarkupElementSelector();

BlogMarkupElementSelector.prototype = {
    getElements: function() {
        /// <summary>
        /// Retrieves all of the HTML elements that contain markup to strip.
        /// </summary>
        /// <returns type="Array" />

        var elements = [];
        
        var user = $.getCurrentUser().userName;
        
        var that = this;
        if (!this._author || this._author !== user) {
            $FX("div.post_body", this._htmlDocument).each(
                function() {
                    if (!that._author) {
                        var author = $FX(this.parentNode).find("strong.author a").html();
                        
                        $.verbose("MARKUP STRIP | Filter: " + author + " ?= " + user);
                        if (author === user) {
                            return;
                        }
                    }
                    elements.push(this);
                }
            );
        }
        else {
            $.verbose("MARKUP STRIP | All blog posts were filtered from consideration because they belong to the user.");
        }
        
        $FX("div.commenttext", this._htmlDocument).each(
            function() {
                var author = $FX(this.parentNode).find("a.username").html();
                
                $.verbose("MARKUP STRIP | Filter: " + author + " ?= " + user);
                if (author === user) {
                    return;
                }
                elements.push(this);
            }
        );
        
        return elements;
    }
};

}});